package net.cgsoft.widget;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * Created by Youga on 2015/12/25.
 */
public class PickerFragment extends DialogFragment {

    public static final String TAG = "PickerFragment";
    PickerFragmentCallBack callBack;
    DatePicker datePicker;
    Button btnCancel, btnConfirm;
    private String mYear, mMonth, mDay;

    public static PickerFragment newInstance(Date date, PickerFragmentCallBack callBack) {
        PickerFragment fragment = new PickerFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("date", date);
        fragment.setArguments(bundle);
        fragment.setPickerFragmentCallBack(callBack);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        if (dialog != null) dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_picker, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Date date = (Date) getArguments().getSerializable("date");
        if (date == null) date = new Date();
        datePicker = (DatePicker) view.findViewById(R.id.datePicker);
        btnCancel = (Button) view.findViewById(R.id.button_cancel);
        btnConfirm = (Button) view.findViewById(R.id.button_confirm);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

        datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                view.clearFocus();
                int month = view.getMonth();
                int day = view.getDayOfMonth();
                mYear = view.getYear() + "";
                mMonth = (month + 1 < 10 ? "0" + (month + 1) : month + 1) + "";
                mDay = (day < 10 ? "0" + day : day) + "";
            }
        });


        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                datePicker.clearFocus();
                int month = datePicker.getMonth();
                int day = datePicker.getDayOfMonth();
                mYear = datePicker.getYear() + "";
                mMonth = (month + 1 < 10 ? "0" + (month + 1) : month + 1) + "";
                mDay = (day < 10 ? "0" + day : day) + "";
                callBack.onDateChanged(mYear, mMonth, mDay);
            }
        });
    }

    public void setPickerFragmentCallBack(PickerFragmentCallBack callBack) {
        this.callBack = callBack;
    }

    public interface PickerFragmentCallBack {
        void onDateChanged(String year, String monthOfYear, String dayOfMonth);
    }
}
